<template>

  <el-select
    v-model="teacherId"
    placeholder="请选择教师"
    filterable
    clearable
    :disabled="disabled"
  >
    <el-option
      v-for="dict in teacherList"
      :key="dict.id"
      :label="dict.teacherName"
      :value="dict.id"
    />
  </el-select>


</template>

<script>
import common from "@/api/collegebase/common";

export default {
  name: "TeacherSelector",
  props: {
    value: {
      type: String,
      default: ''
    },
    collegeId: {
      type: String,
      default: ''
    },

    disabled: {
      type: Boolean,
      default: false
    }

  }
  ,
  model: {
    prop: 'value',
    event: 'change'
  },
  data() {
    return {
      teacherList: [],
      teacherId: '',
    };
  },

  created() {
  },

  watch: {
    collegeId: {
      immediate: true,
      handler(newVal, oldVal) {
        // console.log(newVal, oldVal)
        if (oldVal) {
          this.teacherId = '';
        } else {
          this.teacherId = this.value;
        }

        this.getDepartmentData(newVal);
      }
    },
    teacherId(value) {
      this.$emit('change', value)
    },
    value(value) {
      this.teacherId = value;
    }
  },
  methods: {
    //获取学院
    async getDepartmentData(collegeId) {
      if (null == collegeId || '' == collegeId) {
        this.teacherList = []
        return;
      }
      const departmentsList = await common.getTeacherByCollegeId(collegeId);
      // console.info(departmentsList)
      this.teacherList = departmentsList.data;
    },

    clear() {
      this.$emit("change", '')
    }


  }

}
</script>

